<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')"
             :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="供应商编号" prop="apply_code">
				<el-input v-model="form.apply_code" disabled placeholder="自动生成" clearable autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="供应商分类" prop="apply_cate">
				<el-select v-model="form.apply_cate" filterable style="width: 100%">
					<el-option v-for="item in cateList" :key="item.apply_cate_id" :label="item.apply_cate_name" :value="item.apply_cate_id" />
				</el-select>
			</el-form-item>
			<el-form-item label="供应商名称" prop="apply_name">
				<el-input v-model="form.apply_name" placeholder="供应商名称" clearable></el-input>
			</el-form-item>
            <el-form-item label="供应商等级" prop="apply_grade">
				<el-select v-model="form.apply_grade" filterable style="width: 100%">
					<el-option v-for="item in gradeList" :key="item.id" :label="item.grade_name" :value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="联系人" prop="linkman">
				<el-input v-model="form.linkman" placeholder="联系人" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系电话" prop="mobile">
				<el-input v-model="form.mobile" placeholder="联系电话" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系地址" prop="address">
				<el-input v-model="form.address" placeholder="联系地址" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="system_disable">
				<el-switch v-model="form.system_disable" :active-value="0"
						   :inactive-value="1" inline-prompt active-text="启用" inactive-text="禁用"></el-switch>
			</el-form-item>
			<el-form-item label="简介" prop="desc">
				<el-input type="textarea" v-model="form.desc" placeholder="简介" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增供应商',
				edit: '编辑供应商',
				show: '查看供应商'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				apply_id: "",
				apply_name: "",
				apply_code: "",
				linkman: "",
				mobile: "",
				address: "",
				desc: "",
				system_disable: 0,
				apply_cate: ''
			},
			cateList: [],
			grandList: [],
			//验证规则
			rules: {
				// apply_cate: [
				// 	{ required: true, message: '请选择供应商分类' }
				// ],
				apply_name: [
					{ required: true, message: '请输入供应商名称' }
				],
				// linkman: [
				// 	{ required: true, message: '请输入联系人' }
				// ],
				// mobile: [
				// 	{ required: true, message: '请输入联系电话' }
				// ]
			},
			gradeList:[]
		}
	},
	mounted() {
		this.getCateList();
		this.getGrade();
	},
	methods: {
        async getCateList(){
            var res = await this.$API.apply.applyCate.all.get();
            this.cateList = res.data;
        },
		/**获取供应商等级*/
        async getGrade(){
            var res = await this.$API.apply.applyGrade.all.get();
            this.gradeList = res.data;
        },
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.apply_id) {
                        console.log('编辑了');
						res = await this.$API.apply.apply.edit.post(this.form);
					} else {
						res = await this.$API.apply.apply.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success('操作成功')
					} else {
						this.$alert(res.message, '提醒', {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
            console.log('data :>> ', data);
            this.form.apply_id = data.apply_id
            this.form.apply_cate = data.apply_cate
            this.form.apply_name = data.apply_name
            this.form.apply_code = data.apply_code
            this.form.desc = data.desc
            this.form.linkman = data.linkman
            this.form.mobile = data.mobile
            this.form.system_disable = data.system_disable
            this.form.address = data.address
			this.form.apply_grade = data.apply_grade
		}
	}
}
</script>

<style></style>
